﻿using System.Collections.Generic;

namespace facebook
{
	/// <summary>
	/// fql methods
	/// </summary>
	public class fql
	{
		#region Constructor

		private readonly API _api;
		private bool _useJson = false;

		/// <summary>
		/// Public constructor for facebook.fql
		/// </summary>
		/// <param name="parent">Needs a connected API object for making requests</param>
		public fql(API parent)
		{
			_api = parent;
		}

		#endregion

		#region Properties

		/// <summary>
		/// Determines what format the query results will be returned in: JSON if true; XML otherwise.
		/// </summary>
		public bool UseJson
		{
			get { return _useJson; }
			set { _useJson = value; }
		}

		#endregion

		#region Facebook API

		/// <summary>
		/// Sends a direct FQL query to FB
		/// </summary>
		/// <param name="query">FQL Query</param>
		/// <returns>Result of the FQL query as an XML string</returns> 
		public string query(string query)
		{
			var parameterList = new Dictionary<string, string>(3) {{"method", "facebook.fql.query"}};
			_api.AddRequiredParameter(parameterList, "query", query);

			if (_useJson)
			{
				_api.AddRequiredParameter(parameterList, "format", "json");
			}

            return _api.SendRequest(parameterList, !string.IsNullOrEmpty(_api.SessionKey));
		}

		#endregion
	}
}